/**
 * 角色管理JS操作
 */
$(function() {
/*表单验证：begin*/
	//自定义规则，用法：验证元素上加class="js-pattern-sort"
	if ($.AMUI && $.AMUI.validator) {
		$.AMUI.validator.patterns.sort = /^([0-9]+)$/;
	}
	$("#tmpForm").validator({
		// 域通过验证时回调
		onValid: function(validity) {
		  $(validity.field).closest('.am-form-group').find('.am-alert').hide();
		},
		// 域验证通过时添加的操作，通过该接口可定义各种验证提示
		markValid: function(validity) {
			// this is Validator instance
			var $field  = $(validity.field);
			//add by zxl，只对有required属性的字段进行验证
			if(typeof($field.attr("required"))!="undefined"){
				var options = this.options;
				var $parent = $field.closest('.am-form-group');
				$field.addClass(options.validClass).
				  removeClass(options.inValidClass);

				$parent.addClass('am-form-success').removeClass('am-form-error');

				options.onValid.call(this, validity);
			}
		},
		// 验证出错时的回调， validity 对象包含相关信息，格式通 H5 表单元素的 validity 属性
		onInValid: function(validity) {
		  var $field = $(validity.field);
		  var $group = $field.closest('.am-form-group');
		  var $alert = $group.find('.am-alert');
		  // 使用自定义的提示信息 或 插件内置的提示信息
		  var msg = $field.data('validationMessage') || this.getValidationMessage(validity);

		  if (!$alert.length) {
		    $alert = $("<div class='am-alert am-alert-danger'></div>").hide().
		    appendTo($group.find(".input-msg"));
		  }
		  console.log("onInValid : "+$field.val());
		  $alert.html(msg).show();
		}
	});
	/*表单验证：end*/	
    $(".role-authority-name").click(function(){
		var $parentNode = $(this).parent();
		var level = Number($parentNode.attr("level"));
		var $allParentNextNode = $parentNode.nextUntil(".level-"+level);
		var $iNode = $parentNode.find("i");
		var $lastNode = $allParentNextNode.last();
		$allParentNextNode.each(function(){
			var myLevel = Number($(this).attr("level"));
			var $myINode = $(this).find("i");
			var type = $(this).attr("type");
			if(myLevel > level){
				if($iNode.attr("class") == "am-icon-folder-open"){
					$(this).css("display","none");
					if(type == "folder"){
						$myINode.attr("class","am-icon-folder");
					}
				}else if($iNode.attr("class") == "am-icon-folder"){
					if(type == "folder"){
						$myINode.attr("class","am-icon-folder-open");
					}
					$(this).css("display","");
				}
				if($(this).is($lastNode)){
					if($iNode.attr("class") == "am-icon-folder-open"){
						$iNode.attr("class","am-icon-folder");
					}else if($iNode.attr("class") == "am-icon-folder"){
						$iNode.attr("class","am-icon-folder-open");
					}
				}
			}else{
				if($(this).is($lastNode)){
					if($iNode.attr("class") == "am-icon-folder-open"){
						$iNode.attr("class","am-icon-folder");
					}else if($iNode.attr("class") == "am-icon-folder"){
						$iNode.attr("class","am-icon-folder-open");
					}
				}
				return;
			}
		});
	});

	$(".role-authority-checkall").click(function(){
		var $parentNode = $(this).parent().parent();
		var level = Number($parentNode.attr("level"));
		var $allParentNextNode = $parentNode.nextUntil(".level-"+level);
		var isChecked = $(this).is(":checked");
		$allParentNextNode.each(function(){
			var myLevel = Number($(this).attr("level"));
			var $checks = $(this).children().find("input[type='checkbox']");
			if(myLevel > level){
				return changeCheckedStatus($checks,isChecked);
			}else{
				return;
			}
		});

		var $checks = $(this).parent().prev().find("input[type='checkbox']");
		changeCheckedStatus($checks,isChecked);
	});

	function changeCheckedStatus(checksObj,isChecked){
		checksObj.each(function(){
			if(isChecked){
				$(this).prop("checked",true);
			}else{
				$(this).prop("checked",false);
			}
		});	
	}
});

function node_Click(id, pid, obj) {
	var currentId = id;
	var trRows = $(".nodetr");

	var isshow = $(obj).parent().attr("isshow");//点击的父级tr
	if (isshow == "1") {
		isshow = "0";//关
		$($(obj).find("span")[1]).addClass("folder-closeline");
		$($(obj).find("span")[1]).removeClass("folder-line");

	} else {
		isshow = "1";//开
		$($(obj).find("span")[1]).addClass("folder-line");
		$($(obj).find("span")[1]).removeClass("folder-closeline");
	}
	$(obj).parent().attr("isshow", isshow);

	for (var i = 0; i < trRows.length; i++) {

		var tdObj = trRows[i];
		var nodeValues = $(tdObj).attr("nodevalue");
		var nodeValueArray = nodeValues.split(",");

		if (nodeValueArray[1] == currentId) {

			$(tdObj).attr("isshow", isshow);

			if (isshow == "1") {
				$(tdObj).css("display", "");
				$($(tdObj).find("span")[1]).addClass("folder-line");
				$($(tdObj).find("span")[1]).removeClass(
						"folder-closeline");
			} else {
				$(tdObj).hide();
				$($(tdObj).find("span")[1])
						.addClass("folder-closeline");
				$($(tdObj).find("span")[1]).removeClass("folder-line");
			}
			QueryNode(nodeValueArray[0], trRows, isshow);
		}

	}
}
function QueryNode(pid, trRows2, isopen) {

	for (var i = 0; i < trRows2.length; i++) {
		var nodeValues = $(trRows2[i]).attr("nodevalue");
		var nodeValueArray = nodeValues.split(",");

		if (nodeValueArray[1] == pid) {

			$(trRows2[i]).attr("isshow", isopen);

			if (isopen == "1") {
				$(trRows2[i]).css("display", "");
				$($(trRows2[i]).find("span")[1])
						.addClass("folder-line");
				$($(trRows2[i]).find("span")[1]).removeClass(
						"folder-closeline");

			} else {
				$(trRows2[i]).hide();
				$($(trRows2[i]).find("span")[1]).addClass(
						"folder-closeline");
				$($(trRows2[i]).find("span")[1]).removeClass(
						"folder-line");
			}
			QueryNode(nodeValueArray[0], trRows2, isopen);
		}
	}
}

function RoleCheck(id, isChecked) {

	//alert(id);
	var trRows = $(".nodetr");
	for (var i = 0; i < trRows.length; i++) {
		var tdObj = trRows[i];
		var nodeValues = $(tdObj).attr("nodevalue");
		var nodeValueArray = nodeValues.split(",");
		if (nodeValueArray[0] == id) {
			if (isChecked) {
				$(tdObj).find("input[type='checkbox']").prop("checked",
						true);
			} else {
				$(tdObj).find("input[type='checkbox']").removeAttr(
						"checked");
			}
			RoleCheck2(nodeValueArray[0], trRows, isChecked);
		}
	}
}

function RoleCheck2(pid, trRows2, isChecked) {

	for (var i = 0; i < trRows2.length; i++) {
		var nodeValues = $(trRows2[i]).attr("nodevalue");
		var nodeValueArray = nodeValues.split(",");
		var tdObj = trRows2[i];
		if (nodeValueArray[1] == pid) {

			if (isChecked) {
				$(tdObj).find("input[type='checkbox']").prop("checked",
						true);
			} else {
				$(tdObj).find("input[type='checkbox']").removeAttr(
						"checked");
			}
			RoleCheck2(nodeValueArray[0], trRows2, isChecked);
		}
	}
}